/*
 * @Descripttion:
 * @Author: 张正兴
 * @Date: 2022-03-28 15:59:08
 * @LastEditors: 张正兴
 */
import * as THREE from "three";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass";
import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass";
import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader";
import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js";

/**
 * 渲染器
 */
export function renderer(width: number, height: number) {
  const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  renderer.shadowMap.enabled = true; // 开启阴影
  renderer.shadowMap.type = THREE.BasicShadowMap;
  renderer.setSize(width, height);
  return renderer;
}

/**
 * 后期处理
 */
export function composer(width: number, height: number, renderer: any, scene: any, camera: any) {
  const composer = new EffectComposer(renderer);
  const renderPass = new RenderPass(scene, camera);
  composer.addPass(renderPass);

  // 选中发光
  const outlinePass = new OutlinePass(new THREE.Vector2(width, height), scene, camera);
  outlinePass.visibleEdgeColor.set("#e7b80d");
  composer.addPass(outlinePass);

  // 抗锯齿
  const effectFXAA = new ShaderPass(FXAAShader);
  effectFXAA.uniforms["resolution"].value.set(1 / width, 1 / height);
  composer.addPass(effectFXAA);
  return { composerObj: composer, outlinePass };
}

/**
 * 重新渲染
 *  */
// container.resize = e => {
//   this.camera.aspect = container.clientWidth / container.clientHeight;
//   this.camera.updateProjectionMatrix();
//   this.renderer.setSize(container.clientWidth,container.clientHeight);
// };
